Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Operators

Operators in python

In Python, operators are special symbols that perform operations on values (operands). They manipulate data, control the flow of your program, and combine values to produce new results. Operators are essential building blocks for any Python program. Types of Operators in Python:

Arithmetic Operators

Used for mathematical calculations on numeric operands. ⯁ + (addition): Adds two numbers (e.g., 5 + 3 = 8). ⯁ - (subtraction): Subtracts one number from another (e.g., 10 - 4 = 6). ⯁ * (multiplication): Multiplies two numbers (e.g., 2 * 7 = 14). ⯁ / (division): Divides one number by another (e.g., 12 / 3 = 4). In Python 2, this might result in a float for non-integer division. In Python 3, it always returns a float. ⯁ // (floor division): Divides and returns the largest whole number (integer quotient) (e.g., 11 // 4 = 2). ⯁ % (modulo): Returns the remainder after division (e.g., 13 % 5 = 3). ⯁ ** (exponentiation): Raises a number to a power (e.g., 2 ** 3 = 8).

Assignment Operators

Assign values to variables and potentially perform calculations at the same time. ⯁ = (assignment): Assigns a value to a variable (e.g., x = 10). ⯁ +=, -=, *=, /=, //=, %= (combined assignment): Perform an operation and assign the result (e.g., x += 5 is equivalent to x = x + 5).

Comparison Operators

Compare values and return Boolean (True or False) results. ⯁ == (equal to): Checks if two values are equal (e.g., 5 == 5 is True). ⯁ != (not equal to): Checks if two values are not equal (e.g., 7 != 3 is True). ⯁ < (less than): Checks if one value is less than another (e.g., 2 < 8 is True). ⯁ > (greater than): Checks if one value is greater than another (e.g., 15 > 10 is True). ⯁ <= (less than or equal to): Checks if one value is less than or equal to another (e.g., 4 <= 4 is True). ⯁ >= (greater than or equal to): Checks if one value is greater than or equal to another (e.g., 9 >= 7 is True).

Logical Operators

Used to combine Boolean expressions and control program flow. ⯁ and: Returns True if both operands are True (e.g., (x > 0) and (y < 10)). ⯁ or: Returns True if at least one operand is True (e.g., (x == 5) or (y == 2)). ⯁ not: Inverts the truth value of an operand (e.g., not (age >= 18)).

Identity Operators

Check for object identity, not just value equality. ⯁ is: Returns True if two variables refer to the same object in memory (e.g., a = [1, 2, 3]; b = a; a is b is True). ⯁ is not: Returns True if two variables do not refer to the same object in memory (e.g., x = 5; y = 5; x is not y is True, even though their values are equal).

Membership Operators

Check if a value is present within a sequence (like a list, tuple, or string). ⯁ in: Returns True if a value is found in a sequence (e.g., 'a' in 'apple'). ⯁ not in: Returns True if a value is not found in a sequence (e.g., 10 not in [2, 4, 6] is True).

Bitwise Operators

Perform operations on the binary representation of numbers (useful for low-level programming or working with binary)

  📌TAGS

★python ★ operators

Tutorials